package com.linewow.xhyy.nihaonew.adapter;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.linewow.xhyy.nihaonew.R;
import com.linewow.xhyy.nihaonew.entity.image.ImagesInfo;
import com.peekandpop.shalskar.peekandpop.PeekAndPop;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by LXR on 2016/10/19.
 * 用户推荐可以  上拉加载
 */
public class ImagesInfoAdapterLoad extends BaseQuickAdapter<ImagesInfo> {
    PeekAndPop peekAndPop;
    private TextView intoTv,heartTv;
    private ImageView peekImg;
    private Context context;
    private List<ImagesInfo>data;
    public ImagesInfoAdapterLoad(List<ImagesInfo> data) {
        super(R.layout.item_images_3,data);
    }
    public ImagesInfoAdapterLoad(List<ImagesInfo> data, PeekAndPop peekAndPop) {
        super(R.layout.item_images_3,data);
        this.peekAndPop=peekAndPop;
        this.data=data;
        peekAndPop.setOnGeneralActionListener(new PeekAndPop.OnGeneralActionListener() {
            @Override
            public void onPeek(View longClickView, int position) {
                updatePeek(position);
            }

            @Override
            public void onPop(View longClickView, int position) {

            }
        });
        peekAndPop.addHoldAndReleaseView(R.id.peek_heart_img);
        peekAndPop.addHoldAndReleaseView(R.id.peek_into_img);
        peekAndPop.setOnHoldAndReleaseListener(new PeekAndPop.OnHoldAndReleaseListener() {
            @Override
            public void onHold(View view, int i) {
                if(view.getId()==R.id.peek_heart_img){
                    startWordAnim(heartTv);
                }else if(view.getId()==R.id.peek_into_img){
                    startWordAnim(intoTv);
                }
            }

            @Override
            public void onLeave(View view, int i) {
                Log.e(TAG,"离开了");
            }

            @Override
            public void onRelease(View view, int i) {
                if(view.getId()==R.id.peek_heart_img){
                    Log.e(TAG,"点赞了");
                }else if (view.getId()==R.id.peek_into_img){
                    Log.e(TAG,"进入活动了");
                }
            }
        });
    }

    private void updatePeek(int position) {
        ImagesInfo info=data.get(position);
        View view=peekAndPop.getPeekView();
        intoTv= (TextView) view.findViewById(R.id.peek_into_tv);
        heartTv= (TextView) view.findViewById(R.id.peek_heart_tv);
        peekImg= (ImageView) view.findViewById(R.id.peek_img);
        Picasso.with(context).load(info.path.replace("m.en1on1.com", "huibenabc.com")).error(R.mipmap.err_topic).fit().centerCrop().into(peekImg);
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, ImagesInfo imagesInfo) {
        if(context==null){
            context=baseViewHolder.getConvertView().getContext();
        }
        if(peekAndPop!=null){
            int position=baseViewHolder.getAdapterPosition();
            View baseView=baseViewHolder.convertView;
            baseView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                }
            });
            peekAndPop.addLongClickView(baseView,position);
        }
        ImageView imageView=baseViewHolder.getView(R.id.images_3_image);
        Picasso.with(baseViewHolder.getConvertView().getContext()).load(imagesInfo.path.replace("m.en1on1.com", "huibenabc.com")).error(R.mipmap.err_avatar).fit().into(imageView);
    }

    public void startWordAnim(View view){
        AnimatorSet set=new AnimatorSet();
        set.playTogether(
                ObjectAnimator.ofFloat(view,"translationY",0f,-500f),
                ObjectAnimator.ofFloat(view,"alpha",0,1,0)
        );

        set.setDuration(2000);
        set.start();
    }

}
